package com.clp.inflxudb.util;

import com.clp.inflxudb.core.enums.TsUnit;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;

public class FluxTimeUtil {
    /**
     * 获取指定时间的当日、月、年起始时间
     *
     * @param instant 时间
     * @param unit    单位
     * @return 起始时间
     */
    public static Instant getStartTimeByTsUnit(Instant instant, TsUnit unit) {
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        LocalDateTime startTime;
        switch (unit) {
            case d:
                break;
            case mo:
                localDateTime = localDateTime.with(TemporalAdjusters.firstDayOfMonth());
                break;
            case y:
                localDateTime = localDateTime.with(TemporalAdjusters.firstDayOfYear());
                break;
        }
        startTime = LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MIN);
        return startTime.atZone(zoneId).toInstant();
    }

    public static Instant stringToInstant(String str) {
        if (str == null) {
            return null;
        }
        return Instant.parse(str);
    }
}
